package org.example.classdemo.genericsuperclass;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class BaseService<T,ID extends Serializable> {
    private Class<T> clazz;
    public BaseService(){
        //获取当前对象的超类(父类)的Type
        Type genericSuperClass = this.getClass().getGenericSuperclass();
        if(genericSuperClass instanceof ParameterizedType){
            //参数化类型
            ParameterizedType parameterizedType = (ParameterizedType) genericSuperClass;
            // 这里相当于获取BaseService<User,Integer> 这个类型的 <User类型和Integer类型>
            Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
            this.clazz = (Class<T>) actualTypeArguments[0];
        }
    }
    public Class<T> getClazz(){
        return clazz;
    }
}
